#include "projetInterpreteur.h"
char*monstrcpy(char*a,char*b)
{
    a[0]='\0';
    int i;
    int l=monstrlen(b);
    for(i=0;i<l;i++)
    {
        a[i]=b[i];
    }
    a[l]='\0';
    return a;
}
int variableVrai(FILE*f)
{
  int cmpt3;
  int e;
  char*a=malloc(2*sizeof(char));
  char*src=malloc(256*sizeof(char));
  src[0]='\0';
  a[0]='\0';
  a[1]='\0';
  char*srcg=malloc(512*sizeof(char));
  srcg[0]='\0';
  cmpt3=fgetc(f);
  a[0]=(char)cmpt3;
  while(a[0]==' ')
  {
        srcg=monstrcat(srcg,a);
        cmpt3=fgetc(f);
        a[0]=(char)cmpt3;
  }
  while((a[0]!=';')&&(a[0]!='=')&&(a[0]!='\n')&&(a[0]!='\r')&&(monstrlen(src)<=9))
  {
      src=monstrcat(src,a);
      srcg=monstrcat(srcg,a);
      cmpt3=fgetc(f);
      a[0]=(char)cmpt3;
      if((!strcmp (src, "ent"))||(!strcmp (src, "flott"))||(!strcmp (src, "cour"))||(!strcmp (src, "vide"))||(!strcmp (src, "longue"))||(!strcmp (src, "gflott"))||(!strcmp (src, "car")))
      {
        src[0]='\0';
        cmpt3=fgetc(f);
        a[0]=(char)cmpt3;
        srcg=monstrcat(srcg,a);
        while(a[0]==' ')
        {
            cmpt3=fgetc(f);
            a[0]=(char)cmpt3;
            srcg=monstrcat(srcg,a);
        }
        while((a[0]!=';')&&(a[0]!='=')&&(a[0]!='\n')&&(a[0]!='\r'))
        {
	        if(((a[0]>='a')&&(a[0]<='z'))||((a[0]>='A')&&(a[0]<='Z'))||((a[0]>=0)&&(a[0]<=9))||(a[0]=' ')){}
	        else
            {
                mongetc(f,srcg);
		        return 0;
            }
            cmpt3=fgetc(f);
            a[0]=(char)cmpt3;
	        srcg=monstrcat(srcg,a);
        }
	    mongetc(f,srcg);
	    return 1;
      }
      mongetc(f,srcg);
      return 0;
    }
    mongetc(f,srcg);
    return 0;
}
fct *debutDeVariable2(FILE* f,fct T[])
{
  int cmpt=0;
  int cmpt3;
  int*e;
  int i;
  int cmpt4;
  int b;
  cmpt3=fgetc(f);
  char*a=malloc(2*sizeof(char));
  char*src=malloc(256*sizeof(char));
  char*src2=malloc(256*sizeof(char));
  char*src3=malloc(256*sizeof(char));
  char*src4=malloc(256*sizeof(char));
  char*src5=malloc(256*sizeof(char));
  src5[0]='\0';
  src4[0]='\0';
  src3[0]='\0';
  src2[0]='\0';
  src[0]='\0';
  a[0]='\0';
  a[1]='\0';
  while(a[0]!=('('))
  {
      cmpt3=fgetc(f);
      a[0]=(char)cmpt3;
      src=monstrcat(src,a);
      if((!strcmp (src, "ent"))||(!strcmp (src, "flott"))||(!strcmp (src, "cour"))||(!strcmp (src, "vide"))||(!strcmp (src, "longue"))||(!strcmp (src, "gflott"))||(!strcmp (src, "car")))
      {
        cmpt3=fgetc(f);
        a[0]=(char)cmpt3;
        while(a[0]==' ')
        {
            cmpt3=fgetc(f);
            a[0]=(char)cmpt3;
        }
        src4=monstrcpy(src4,src);
        while(a[0]!='(')
        {
          src2=monstrcat(src2,a);
	      cmpt3=fgetc(f);
	      a[0]=(char)cmpt3;
	      while(a[0]==' ')
          {
            cmpt3=fgetc(f);
            a[0]=(char)cmpt3;
          }
        }

      }
      for(i=0;i<10;i++)
      {
        if((!cmpt4)&&(T[i].name==NULL))
        {
	      T[i].type=monstrcat(T[i].type,src4);
	      T[i].name=monstrcat(T[i].name,src2);
	      cmpt4=42;
	      b=i;
        }
      }
  }
  src5[0]='\0';
  src4[0]='\0';
  src3[0]='\0';
  src2[0]='\0';
  src[0]='\0';
  while(a[0]!=')')
  {
      cmpt3=fgetc(f);
      a[0]=(char)cmpt3;
      while(a[0]==' ')
      {
        cmpt3=fgetc(f);
        a[0]=(char)cmpt3;
      }
      src=monstrcat(src,a);
      if((!strcmp (src, "ent"))||(!strcmp (src, "flott"))||(!strcmp (src, "cour"))||(!strcmp (src, "vide"))||(!strcmp (src, "longue"))||(!strcmp (src, "gflott"))||(!strcmp (src, "car")))
      {
            src4=monstrcpy ( src4, src);
            while(a[0]==' ')
            {
                cmpt3=fgetc(f);
                a[0]=(char)cmpt3;
            }
            while((a[0]!=',')||(a[0]!=')')||(a[0]!='='))
            {
                src2=monstrcat(src2,a);
                cmpt3=fgetc(f);
                a[0]=(char)cmpt3;
                while(a[0]==' ')
                {
                    cmpt3=fgetc(f);
                    a[0]=(char)cmpt3;
                }

            }
	        if(a[0]=='=')
            {
                cmpt3=fgetc(f);
                a[0]=(char)cmpt3;
                while((a[0]!=',')||(a[0]!=')'))
                {
                    while(a[0]==' ')
                    {
                        cmpt3=fgetc(f);
                        a[0]=(char)cmpt3;
                    }
                    src5=monstrcat(src5,a);
                    cmpt3=fgetc(f);
                    a[0]=(char)cmpt3;
                }
            }
            if(src5[0]!='\0')
            {
                T[i].var=createnodi(T[i].var, src2, src5, src4);
            }
            else
            {
                T[i].var=createnodi(T[i].var, src2, NULL, src4);
            }
            src5[0]='\0';
            src4[0]='\0';
            src3[0]='\0';
            src2[0]='\0';
            src[0]='\0';
        }
  }
  free(src);
  free(src2);
  free(src3);
  free(src4);
  free(src5);
  return T;
}
fct *typeDansFonction(FILE*f,fct T[])
{
  int cmpt3;
  int cmpt=0;
  int i,j,l;
  int*e;
  cmpt3=fgetc(f);
  char*a=malloc(2*sizeof(char));
  char*src=malloc(256*sizeof(char));
  char*src2=malloc(256*sizeof(char));
  char*src3=malloc(256*sizeof(char));
  src[0]='\0';
  src3[0]='\0';
  src2[0]='\0';
  a[0]=(char)cmpt3;
  a[1]='\0';
  while(a[0]==' ')
  {
        cmpt3=fgetc(f);
        a[0]=(char)cmpt3;
  }
  src=monstrcat(src,a);
  while((a[0]=='\n')||(a[0]=='\r'))
  {
      cmpt3=fgetc(f);
      a[0]=(char)cmpt3;
      src=monstrcat(src,a);
      if((!strcmp (src, "ent"))||(!strcmp (src, "flott"))||(!strcmp (src, "cour"))||(!strcmp (src, "vide"))||(!strcmp (src, "longue"))||(!strcmp (src, "gflott"))||(!strcmp (src, "car")))
      {
        while(a[0]==' ')
        {
            cmpt3=fgetc(f);
            a[0]=(char)cmpt3;
        }
        while((a[0]=='=')||(a[0]==';')||(a[0]==' '))
        {
            src2=monstrcat(src2,a);
            cmpt3=fgetc(f);
            a[0]=(char)cmpt3;
        }
        while(a[0]==' ')
        {
            cmpt3=fgetc(f);
            a[0]=(char)cmpt3;
        }
	    if(a[0]=='=')
        {
            cmpt3=fgetc(f);
            a[0]=(char)cmpt3;
            while(a[0]==' ')
            {
                cmpt3=fgetc(f);
                a[0]=(char)cmpt3;
            }
            while(a[0]==';')
            {
                src3=monstrcat(src3,a);
                cmpt3=fgetc(f);
                a[0]=(char)cmpt3;
            }

        }
	    for(i=0;i<10;i++)
        {
            if((!cmpt)&&(T[i].name==NULL))
            {
                cmpt=1;
                j=i-1;
            }
        }
        if(src3[0]!='\0')
        {
            T[j].var=createnodi(T[j].var, src2, src3, src);
        }
        else
        {
            T[j].var=createnodi(T[j].var, src2, NULL, src);
        }
        src3[0]='\0';
        src2[0]='\0';
        src[0]='\0';
        }
    }
    free(src);
    free(src2);
    free(src3);
    return T;
}
fct *Tab()
{
  int i;
  fct T[10];
  for(i=0;i<10;i++)
    {
      T[i].name=NULL;
      T[i].type=NULL;
      T[i].var=NULL;
      T[i].contenu=NULL;
    }
  return T;
}



int parser2(FILE*f,int*e)
{
  char*s=malloc(256*sizeof(char));
  int i=0;
  int j=0;
  int cmpt=0;
  int cmpt2=0;
  char*a=malloc(2*sizeof(char));
  int tmp;
  fct *T;
  T=Tab();
  char*src=malloc(256*sizeof(char));
  char*src2=malloc(256*sizeof(char));
  src2[0]='\0';
  src[0] ='\0';
  s[0]='\0';
  a[1]='\0';
  while((a[0])!=EOF)
  {
      f=supprimeEspace(f,e);
      T=debutDeVariable2(f, T);
      tmp=fgetc(f);
      a[0]=(char)tmp;
      while(a[0]==' ')
      {
        tmp=fgetc(f);
        a[0]=(char)tmp;
      }
      if(a[0]=='{')
      {
        cmpt2++;
        while(cmpt2)
        {
            tmp=fgetc(f);
            a[0]=(char)tmp;
            src2=monstrcat(src2,a);
            if(a[0]=='{')
            {
                cmpt2++;
            }
            else if(a[0]=='}')
            {
                cmpt2--;
            }
            if((a[0]=='\n')||(a[0]=='\r')||(a[0]==';'))
            {
                if(variableVrai(f)==1)
                {
                    T=typeDansFonction(f, T);
                }
            }
	    }
	    for(i=0;i<10;i++)
        {
            if((!cmpt)&&(T[i].name==NULL))
            {
                cmpt=1;
                j=i-1;
            }
        }
        T[j].contenu=monstrcat(T[j].contenu,src2);
	    free(src2);
      }
    }
    free(src);
    //affiche(T);
    return 1;
}
void affiche(fct *t)
{
  int i;
  for(i = 0; i<= 2; i++)
    {
      affiche2(t[i].var);
    }
}
void affiche2(nodi *abr)
{
  if(abr->fg!=NULL)
    affiche2(abr->fg);

  printf("%s\n", abr->clef);

  if(abr->fd!=NULL)
    affiche2(abr->fd);
}
